/**
 * 考勤详细信息列表attendDetailList.js
 * 版本号： v1.0.0
 */
var currentYear = "";
var currentMonth = "";
var nowMonth= "";
var idCard = "";
var projectId = "";
//考勤列表加载完成标志
var loadFlag = true;
$(function(){
    var date = new Date();
    currentYear = date.getFullYear();
    currentMonth = date.getMonth()+1;
    nowMonth = date.getMonth()+1;
    //获取卡号
    idCard = $('#idCard_').val();
    projectId = $('#projectId_').val();
    //考勤人员详情列表
    document.getElementById("attendDetail-dataList").src = encodeURI(CONTEXT_PATH + "/attendDetail/index.do?idCard=" + idCard+
    "&projectId=" + projectId);
});

/**
 * 点击箭头事件
 * @param arrow
 */
function arrowClick(arrow){
    if(loadFlag){
        loadFlag = false;
        loadingFun();
        //左箭头
        if(arrow == 'left'){
            //月份减一
            currentMonth = parseInt(currentMonth) - 1;
            if(currentMonth == 0){
                loadEndFun();
                $.messager.show({ title:"系统提示", msg: "统计数据只展示本年", timeout:3000, showType:"slide" });
                loadFlag = true;
                currentMonth = "1";
                return;
            }
            loadDate(currentMonth,"prev");
        }else{
            //月份加一
            currentMonth = parseInt(currentMonth) + 1;
            if(currentMonth > nowMonth){
                loadEndFun();
                $.messager.show({ title:"系统提示", msg: "数据统计不能大于当前月份", timeout:3000, showType:"slide" });
                loadFlag = true;
                currentMonth = nowMonth;
                return;
            }
            loadDate(currentMonth,"next");
        }
    }else{
        $.messager.show({ title:"系统提示", msg: "考勤列表未加载完成,请等待", timeout:3000, showType:"slide" });
    }
}

/**
 * 刷新页面数据
 * @param disposeMonth
 */
function loadDate(disposeMonth,monthFlag){
    var changeMonth =  getChangeMonth(disposeMonth);
    $('#month_').text(changeMonth);
    var mixDay = getDaysInMonth(currentYear,currentMonth);
    var strMonth = getStrValue(currentMonth);
    var strDay = getStrValue(mixDay);
    var startTime = currentYear + "-" + strMonth + "-01 00:00:00";
    var endTime = currentYear + "-" + strMonth + "-" + strDay + " 23:59:59";
    //加载出勤统计及日历
    $.ajax({
        url: CONTEXT_PATH + "/attend/getAttendDetailInfo.do",
        data: {idCard:idCard,projectId:projectId,startTime:startTime,endTime:endTime},
        type: "post",
        dateType:'json',
        success: function (data) {
            $("#totalAttendLength_").text(data.totalAttendLength);
            $("#workerDays_").text(data.workerDays);
            $("#supplementCard_").text(data.supplementCard);

            var calendar = $("#calendar").data("calendar");
            var timeDateOrgin = data.timeList;
            var timeDate = [];
            for(var i=0;i<timeDateOrgin.length;i++){
                var obj = {};
                obj["id"] = timeDateOrgin[i].id;
                obj["title"] = timeDateOrgin[i].title;
                obj["class"] = timeDateOrgin[i].classTemp;
                obj["start"] = timeDateOrgin[i].start;
                obj["end"] = timeDateOrgin[i].end;
                timeDate.push(obj)
            }
            var options = $.extend(calendar.options , {events_source: timeDate});
            $("#calendar").calendar(options);
            if(monthFlag == "prev"){
                calendar.navigate("prev");
            }else{
                calendar.navigate("next");
            }
            loadEndFun();
        }
    });
    //加载考勤列表数据
    var frm = document.getElementById('attendDetail-dataList');
    frm.src = encodeURI(CONTEXT_PATH + "/attendDetail/index.do?idCard=" + idCard + '&projectId=' + projectId +
        '&startTime=' + startTime + '&endTime=' + endTime);
}

/**
 * 考勤数据加载完成后月份才能切换
 */
function loadListCompanly() {
    loadFlag = true;
}


/**
 * 月份转换
 * @param month
 * @returns {*}
 */
function getChangeMonth(month){
    var monthMap = {
        1:"一月",2:"二月",3:"三月",4:"四月",5:"五月",6:"六月",7:"七月",8:"八月",9:"九月",10:"十月",11:"十一月",12:"十二月"
    }
    return monthMap[month];
}


/**
 * 获取某年某月有多少天
 * @returns {number}
 */
function getDaysInMonth(year,month){
    var month1 = parseInt(month,10);
    var temp= new Date(year,month1,0);
    return temp.getDate();
}

/**
 * 开始加载
 */
function loadingFun(){
    $(".forMask").mask("数据获取中，请稍候…");
}
/**
 * 结束加载
 */
function loadEndFun(){
    $(".forMask").unmask();
}

/**
 * 日期处理，小于10的在前加0
 * @param value
 * @returns {string|number}
 */
function getStrValue(value){
    var intValue = parseInt(value);
    if(intValue < 10){
        intValue = "0" + intValue;
    }else{
        intValue = "" + intValue;
    }
    return intValue;
}


